﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Collections;
using System.Data;
using System.Configuration;
using UFC.Common;

namespace UFC.DBUtility
{
    public class OracleHelper
    {
        #region 公用方法
        /// <summary>
        /// 查询某个表中某个字段的最大值
        /// 查询的字段必须可以转换成整型
        /// </summary>
        /// <param name="FieldName">字段名</param>
        /// <param name="TableName">表名</param>
        /// <returns></returns>
        public static int GetMaxID(string FieldName, string TableName)
        {
            string strsql = "select max(" + FieldName + ")+1 from " + TableName;
            object obj = GetSingle(strsql);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }
        /// <summary>
        /// 查询某些数据是否存在
        /// </summary>
        /// <param name="strSql">查询语句</param>
        /// <param name="cmdParms">参数集合</param>
        /// <returns></returns>
        public static bool Exists(string strSql, params IDataParameter[] cmdParms)
        {
            object obj = GetSingle(strSql, cmdParms);
            int cmdresult;
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 新建OracleParameter参数
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="strValue"></param>
        /// <param name="oracleType"></param>
        public static OracleParameter NewOracleParameter(string strName, object strValue, OracleType oracleType)
        {
            OracleParameter parameter;
            parameter = new OracleParameter(strName, oracleType);
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = strValue;
            return parameter;
        }
        #endregion

        #region  执行简单SQL语句

        /// <summary>
        /// 执行SQL语句，返回影响的记录数
        /// 例如数据添加、删除、更新操作
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString)
        {
            //string strConn = "Data Source=MESWIPDB;User ID=insitedb1;Password=insitedb1";

            using (OracleConnection connection = new OracleConnection(GetConnectionString()))
            {
                using (OracleCommand cmd = new OracleCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (OracleException E)
                    {
                        connection.Close();
                        LogHelp.WriteLog("执行Sql：" + E.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return -1;
        }

        /// <summary>
        /// 执行多条SQL语句，实现数据库事务。
        /// 当所有的执行语句全部执行成功，没有异常后才能进行事务的提交
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</param>		
        public static void ExecuteSqlTran(ArrayList SQLStringList)
        {
            using (OracleConnection conn = new OracleConnection(GetConnectionString()))
            {
                conn.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = conn;
                OracleTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    for (int n = 0; n < SQLStringList.Count; n++)
                    {
                        string strsql = SQLStringList[n].ToString();
                        if (strsql.Trim().Length > 1)
                        {
                            cmd.CommandText = strsql;
                            cmd.ExecuteNonQuery();
                        }
                    }
                    tx.Commit();//事务提交
                }
                catch (OracleException E)
                {
                    tx.Rollback();//事务回滚
                    LogHelp.WriteLog("事务回滚：" + E.Message);
                }
                finally
                {
                    cmd.Dispose();
                    conn.Close();
                }
            }
        }

        /// <summary>
        /// 执行多条SQL语句，实现数据库事务。存在相同SQL，
        /// </summary>
        /// <param name="SQLStringList">SQL语句的哈希表（key为sql语句，value是该语句的OracleParameter[]）</param>
        public static void ExecuteSqlTran(List<object[]> SQLStringList)
        {
            using (OracleConnection conn = new OracleConnection(GetConnectionString()))
            {
                conn.Open();
                using (OracleTransaction trans = conn.BeginTransaction())
                {
                    OracleCommand cmd = new OracleCommand();
                    try
                    {
                        //循环
                        foreach (object[] myDE in SQLStringList)
                        {
                            string cmdText = "";
                            if (myDE.Length > 0)
                                cmdText = myDE[0].ToString();//获得键值

                            OracleParameter[] cmdParms = null;
                            if (myDE.Length > 1)
                                cmdParms = (OracleParameter[])myDE[1];//通过键值获得值
                            PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
                            int val = cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();

                        }
                        //事务提交
                        trans.Commit();
                    }
                    catch (OracleException E)
                    {
                        trans.Rollback();
                        LogHelp.WriteError("数据库事务", E);
                        //throw;
                    }
                }
            }
        }

        /// <summary>
        /// 执行带一个存储过程参数的的SQL语句。
        /// </summary>
        /// <param name="SQLString">SQL语句/或者是存储过程</param>
        /// <param name="content">参数内容,比如一个字段是格式复杂的文章，有特殊符号，可以通过这个方式添加</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, string content)
        {
            using (OracleConnection connection = new OracleConnection(GetConnectionString()))
            {
                OracleCommand cmd = new OracleCommand(SQLString, connection);
                OracleParameter myParameter = new OracleParameter("content", OracleType.Blob);
                myParameter.Value = content;
                cmd.Parameters.Add(myParameter);
                try
                {
                    connection.Open();
                    int rows = cmd.ExecuteNonQuery();
                    return rows;
                }
                catch (OracleException E)
                {
                    LogHelp.WriteError("执行ExecuteSql报错", E);
                }
                finally
                {
                    cmd.Dispose();
                    connection.Close();
                }
            }
            return -1;
        }
        /// <summary>
        /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
        /// </summary>
        /// <param name="strSQL">SQL语句</param>
        /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
        {
            using (OracleConnection connection = new OracleConnection(GetConnectionString()))
            {
                OracleCommand cmd = new OracleCommand(strSQL, connection);
                OracleParameter myParameter = new OracleParameter("fs", OracleType.Blob);
                myParameter.Value = fs;
                cmd.Parameters.Add(myParameter);
                try
                {
                    connection.Open();
                    int rows = cmd.ExecuteNonQuery();
                    return rows;
                }
                catch (OracleException E)
                {
                    LogHelp.WriteError("执行ExecuteSqlInsertImg报错", E);
                }
                finally
                {
                    cmd.Dispose();
                    connection.Close();
                }
            }
            return -1;
        }

        /// <summary>
        /// 执行一条计算查询结果语句，返回查询结果（object）。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果（object）</returns>
        public static object GetSingle(string SQLString)
        {
            using (OracleConnection connection = new OracleConnection(GetConnectionString()))
            {
                using (OracleCommand cmd = new OracleCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        object obj = cmd.ExecuteScalar();//统计功能（一行一列）
                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                        {
                            return null;
                        }
                        else
                        {
                            return obj;
                        }
                    }
                    catch (OracleException E)
                    {
                        connection.Close();
                        LogHelp.WriteError("执行GetSingle报错", E);
                    }
                    finally
                    {
                        cmd.Dispose();
                        connection.Close();
                    }
                }
            }
            return null;
        }
        /// <summary>
        /// 执行查询语句，返回OracleDataReader
        /// </summary>
        /// <param name="strSQL">查询语句</param>
        /// <returns>OracleDataReader</returns>
        public static OracleDataReader ExecuteReader(string strSQL)
        {
            OracleConnection connection = new OracleConnection(GetConnectionString());
            OracleCommand cmd = new OracleCommand(strSQL, connection);
            try
            {
                connection.Open();
                OracleDataReader myReader = cmd.ExecuteReader();
                return myReader;
            }
            catch (OracleException E)
            {
                LogHelp.WriteError("事务回滚", E);
                return null;
                //throw new Exception(e.Message);
            }
            finally
            {
                cmd.Dispose();
                connection.Close();
            }

        }
        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString)
        {
            DataSet ds = new DataSet();
            try
            {
                using (OracleConnection connection = new OracleConnection(GetConnectionString()))
                {
                    connection.Open();
                    OracleDataAdapter command = new OracleDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                    connection.Close();
                }
                return ds;
            }
            catch (Exception ex)
            {
                LogHelp.WriteError(SQLString, ex);
                return ds;
            }
   
        }

        public static DataTable QueryTable(string SQLString, params IDataParameter[] cmdParms)
        {
            using (OracleConnection connection = new OracleConnection(GetConnectionString()))
            {
                OracleCommand cmd = new OracleCommand();
                PrepareCommand(cmd, connection, null, SQLString, (OracleParameter[])cmdParms);
                using (OracleDataAdapter da = new OracleDataAdapter(cmd))
                {
                    DataTable ds = new DataTable();
                    try
                    {
                        da.Fill(ds);
                        cmd.Parameters.Clear();
                    }
                    catch (OracleException ex)
                    {
                        LogHelp.WriteError("Query", ex);
                        //throw new Exception(ex.Message);
                    }
                    return ds;
                }
            }
        }
        #endregion

        #region 执行带参数的SQL语句

        /// <summary>
        /// 执行SQL语句，返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, params IDataParameter[] cmdParms)
        {
            using (OracleConnection connection = new OracleConnection(GetConnectionString()))
            {
                using (OracleCommand cmd = new OracleCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, (OracleParameter[])cmdParms);
                        int rows = cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        return rows;
                    }
                    catch (OracleException E)
                    {
                        LogHelp.WriteError("ExecuteSql", E);
                        throw new Exception(E.Message);
                    }
                }
            }
            return -1;
        }
        /// <summary>
        /// 执行多条SQL语句，实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">SQL语句的哈希表（key为sql语句，value是该语句的OracleParameter[]）</param>
        public static void ExecuteSqlTran(Hashtable SQLStringList)
        {
            using (OracleConnection conn = new OracleConnection(GetConnectionString()))
            {
                conn.Open();
                using (OracleTransaction trans = conn.BeginTransaction())
                {
                    OracleCommand cmd = new OracleCommand();
                    try
                    {
                        //循环
                        foreach (DictionaryEntry myDE in SQLStringList)
                        {
                            string cmdText = myDE.Key.ToString();//获得键值
                            OracleParameter[] cmdParms = (OracleParameter[])myDE.Value;//通过键值获得值
                            PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
                            int val = cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            //事务提交
                            trans.Commit();
                        }
                    }
                    catch (OracleException E)
                    {
                        trans.Rollback();
                        LogHelp.WriteError("数据库事务", E);
                        //throw;
                    }
                }
            }
        }

        /// <summary>
        /// 执行一条计算查询结果语句，返回查询结果（object）。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果（object）</returns>
        public static object GetSingle(string SQLString, params IDataParameter[] cmdParms)
        {
            using (OracleConnection connection = new OracleConnection(GetConnectionString()))
            {
                using (OracleCommand cmd = new OracleCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, (OracleParameter [])cmdParms);
                        object obj = cmd.ExecuteScalar();
                        cmd.Parameters.Clear();
                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                        {
                            return null;
                        }
                        else
                        {
                            return obj;
                        }
                    }
                    catch (OracleException e)
                    {
                        LogHelp.WriteError("执行GetSingle", e);
                        //throw new Exception(e.Message);
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 执行查询语句，返回OracleDataReader
        /// </summary>
        /// <param name="strSQL">查询语句</param>
        /// <returns>OracleDataReader</returns>
        public static OracleDataReader ExecuteReader(string SQLString, params IDataParameter[] cmdParms)
        {
            OracleConnection connection = new OracleConnection(GetConnectionString());
            OracleCommand cmd = new OracleCommand();
            try
            {
                PrepareCommand(cmd, connection, null, SQLString, (OracleParameter[])cmdParms);
                OracleDataReader myReader = cmd.ExecuteReader();
                cmd.Parameters.Clear();
                return myReader;
            }
            catch (OracleException e)
            {
                LogHelp.WriteError("ExecuteReader", e);
                //throw new Exception(e.Message);
            }
            return null;
        }

        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString, params IDataParameter[] cmdParms)
        {  
            using (OracleConnection connection = new OracleConnection(GetConnectionString()))
            {
                OracleCommand cmd = new OracleCommand();
                PrepareCommand(cmd, connection, null, SQLString, (OracleParameter[])cmdParms);
                using (OracleDataAdapter da = new OracleDataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    try
                    {
                        da.Fill(ds, "ds");
                        cmd.Parameters.Clear();
                    }
                    catch (OracleException ex)
                    {
                        LogHelp.WriteError("Query", ex);
                        //throw new Exception(ex.Message);
                    }
                    return ds;
                }
            }
        }

        /// <summary>
        ///在存在事务的情况下，新建数据库连接，执行sql语句，此处是关键，上面的所有功能操作都沿此而进行
        /// 此处的sql语句可能是增加、删除、更新、查询等等操作
        /// Prepare a command for execution
        /// </summary>
        /// <param name="cmd">OracleCommand对象</param>
        /// <param name="conn">OracleConnection对象</param>
        /// <param name="trans">OracleTransaction对象</param>
        /// <param name="cmdType">Cmd type e.g. 存储过程或者sql语句</param>
        /// <param name="cmdText">Command text, e.g. Select * from Products</param>
        /// <param name="cmdParms">OracleParameters to use in the command</param>
        private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, string cmdText, OracleParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
                cmd.Transaction = trans;
            cmd.CommandType = CommandType.Text;//cmdType;
            if (cmdParms != null)
            {
                foreach (OracleParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }

        #endregion

        #region 存储过程操作

        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>OracleDataReader</returns>
        public static OracleDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
        {
            OracleConnection connection = new OracleConnection(GetConnectionString());
            OracleDataReader returnReader;
            connection.Open();
            OracleCommand command = BuildQueryCommand(connection, storedProcName, parameters);
            command.CommandType = CommandType.StoredProcedure;
            returnReader = command.ExecuteReader();
            return returnReader;
        }

        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <param name="tableName">DataSet结果中的表名</param>
        /// <returns>DataSet</returns>
        public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
        {
            OracleConnection connection = null;
            OracleDataAdapter sqlDA = null;
            OracleTransaction trans = null;
            try
            {
                using (connection = new OracleConnection(GetConnectionString()))
                {
                    DataSet dataSet = new DataSet();
                    connection.Open();
                    sqlDA = new OracleDataAdapter();
                    sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
                    //trans = connection.BeginTransaction(IsolationLevel.ReadCommitted);
                    //if (trans != null)
                    {
                        // sqlDA.SelectCommand.Transaction = trans;
                        sqlDA.Fill(dataSet, tableName);
                        //trans.Commit();
                    }
                    connection.Close();
                    //trans.Dispose();
                    return dataSet;
                }
            }
            catch (Exception RunProcedureEx)
            {
                //Log.WriteLog(RunProcedureEx.Message);
                return null;
                //throw RunProcedureEx;
            }
            finally
            {
                if (sqlDA != null)
                    sqlDA.Dispose();
                //trans.Dispose();
                if (connection.State == ConnectionState.Open)
                    connection.Close();
            }
        }
        /// <summary>
        /// 构建 OracleCommand 对象(用来返回一个结果集，而不是一个整数值)
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>OracleCommand</returns>
        protected static OracleCommand BuildQueryCommand(OracleConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            OracleCommand command = new OracleCommand(storedProcName, connection);
            command.CommandType = CommandType.StoredProcedure;
            foreach (OracleParameter parameter in parameters)
            {
                command.Parameters.Add(parameter);
            }
            return command;
        }

        /// <summary>
        /// 执行存储过程，返回影响的行数		
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <param name="rowsAffected">影响的行数</param>
        /// <returns></returns>
        public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
        {
            int result = 0;
            try
            {
                using (OracleConnection connection = new OracleConnection(GetConnectionString()))
                {

                    connection.Open();
                    OracleCommand command = BuildIntCommand(connection, storedProcName, parameters);
                    OracleTransaction trans = connection.BeginTransaction(IsolationLevel.ReadCommitted);
                    if (trans != null)
                    {
                        command.Transaction = trans;
                        result = command.ExecuteNonQuery();
                        trans.Commit();
                    }
                }
            }
            catch (Exception RunProcedureEx)
            {
                //Log.WriteLog(RunProcedureEx.Message);
                //throw RunProcedureEx;
            }
            rowsAffected = result;
            return result;
        }

        /// <summary>
        /// 执行存储过程，查询，并且有输出参数	
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <param name="rowsAffected">影响的行数</param>
        /// <returns></returns>
        public static string RunProcedureQuery(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
        {
            string outString = "";
            try
            {
                using (OracleConnection connection = new OracleConnection(GetConnectionString()))
                {

                    connection.Open();
                    OracleCommand command = BuildIntCommand(connection, storedProcName, parameters);
                    OracleTransaction trans = connection.BeginTransaction(IsolationLevel.ReadCommitted);
                    if (trans != null)
                    {
                        command.Transaction = trans;
                        command.ExecuteNonQuery();
                        trans.Commit();
                    }

                    outString = command.Parameters["TempID"].Value.ToString();//输出参数


                }
            }
            catch (Exception RunProcedureEx)
            {
                //Log.WriteLog(RunProcedureEx.Message);
                //throw RunProcedureEx;
            }
            rowsAffected = 0;
            return outString;
        }

        /// <summary>
        /// 创建 OracleCommand 对象实例(用来返回一个整数值)	
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>OracleCommand 对象实例</returns>
        private static OracleCommand BuildIntCommand(OracleConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            OracleCommand command = BuildQueryCommand(connection, storedProcName, parameters);
            return command;
        }

        /// <summary>
        /// 执行存储过程，没有返回任何信息，如插入数据等
        /// </summary>
        /// <param name="procname"></param>
        /// <param name="cmdParms"></param>
        public static void ExecuteProc(string procname, params OracleParameter[] cmdParms)
        {
            using (OracleConnection SqlConn = new OracleConnection(GetConnectionString()))
            {
                if (SqlConn.State == ConnectionState.Closed)
                {
                    SqlConn.Open();
                }
                OracleDataAdapter sdar = new OracleDataAdapter();
                sdar.SelectCommand = new OracleCommand();
                sdar.SelectCommand.Connection = SqlConn;
                sdar.SelectCommand.CommandText = procname;
                sdar.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (cmdParms != null)
                {
                    foreach (OracleParameter parm in cmdParms)
                        sdar.SelectCommand.Parameters.Add(parm);
                }
                int i = sdar.SelectCommand.ExecuteNonQuery();
                sdar.Dispose();
            }

        }

        /// <summary>
        /// 执行参数化的Sql语句,返回DataTable
        /// </summary>
        /// <param name="p_ProcedureName">sql语句</param>
        /// <param name="p_OracleParameterArray">Parm数组</param>
        /// <returns>返回DataTable</returns>
        public static DataTable ExeParameDataTable(string p_ProcedureName, OracleParameter[] p_OracleParameterArray)
        {
            try
            {
                using (OracleConnection SqlConn = new OracleConnection(GetConnectionString()))
                {
                    if (SqlConn.State == ConnectionState.Closed)
                    {
                        SqlConn.Open();
                    }
                    DataTable dt = new DataTable();
                    OracleDataAdapter da = new OracleDataAdapter(p_ProcedureName, SqlConn);

                    foreach (OracleParameter Sq in p_OracleParameterArray)
                    {
                        da.SelectCommand.Parameters.Add(Sq);
                    }
                    da.Fill(dt);
                    da.SelectCommand.Parameters.Clear();
                    return dt;
                }
            }
            catch (Exception ex)
            {
                //Log.WriteLog(ex.Message);
                return null;
                //throw ex;
            }
        }
        /// <summary>
        /// 将金额从小写转换成大写字母书写
        /// </summary>
        /// <param name="endMoney">要转化的金额数目</param>
        /// <returns>返回金额大写书写</returns>
        public static string ReturnMaxMoney(double endMoney)
        {
            try
            {
                string returnstring = "";
                using (OracleConnection SqlConn = new OracleConnection(GetConnectionString()))
                {
                    if (SqlConn.State == ConnectionState.Closed)
                    {
                        SqlConn.Open();
                    }
                    using (OracleCommand comm = new OracleCommand("MoneytoChinese22", SqlConn))
                    {
                        comm.CommandType = CommandType.StoredProcedure;
                        //输入参数
                        comm.Parameters.Add("ChangeMoney", OracleType.Number);
                        //输出参数
                        comm.Parameters.Add("ReturnDaXie", OracleType.VarChar, 50);
                        //comm.Parameters["ReturnDaXie"].Direction = ParameterDirection.ReturnValue;
                        comm.Parameters["ChangeMoney"].Value = endMoney;
                        comm.Parameters["ReturnDaXie"].Direction = ParameterDirection.Output;
                        comm.ExecuteNonQuery();//执行存储过程
                        returnstring = comm.Parameters["ReturnDaXie"].Value.ToString();
                    }
                }
                return returnstring;
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        #endregion

        #region  执行连接到其他服务器的Sql,机匣正式服务器用 add：Wangjh 20170925
        /// <summary>
        /// 连接到其他服务器,执行查询语句，返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString, Boolean IsOtherConn)
        {
            //using (OracleConnection connection = new OracleConnection(GetConnectionString()))
            //{
            OracleConnection connection;
            if (IsOtherConn) connection = new OracleConnection(GetJXConnectionString());
            else connection = new OracleConnection(GetConnectionString());
            DataSet ds = new DataSet();
            try
            {
                connection.Open();
                OracleDataAdapter command = new OracleDataAdapter(SQLString, connection);
                command.Fill(ds, "ds");
                connection.Close();
            }
            catch (OracleException ex)
            {
                //Log.WriteLog(ex.Message);
                //throw new Exception(ex.Message);
            }
            finally
            {
                connection.Close ();
            }
            return ds;
            //}
        }
        /// <summary>
        ///连接到其他服务器, 执行SQL语句，返回影响的记录数
        /// 例如数据添加、删除、更新操作
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, Boolean IsOtherConn)
        {
            //string strConn = "Data Source=insitedb;User ID=insitedbtest;Password=insitedbtest";

            //using (OracleConnection connection = new OracleConnection(GetConnectionString()))
            //{
            OracleConnection connection;
            if (IsOtherConn) connection = new OracleConnection(GetJXConnectionString());
            else connection = new OracleConnection(GetConnectionString());
            using (OracleCommand cmd = new OracleCommand(SQLString, connection))
            {
                try
                {
                    connection.Open();
                    int rows = cmd.ExecuteNonQuery();
                    return rows;
                }
                catch (OracleException E)
                {
                    connection.Close();
                    LogHelp.WriteLog("执行Sql：" + E.Message);
                    //throw new Exception(E.Message);
                }
                finally
                {
                    cmd.Dispose();
                    connection.Close();
                }
            }
            //}
            return -1;
        }
        /// <summary>
        /// 连接到其他服务器,执行SQL语句，返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, Boolean IsOtherConn, params OracleParameter[] cmdParms)
        {
            //using (OracleConnection connection = new OracleConnection(GetConnectionString()))
            //{
            OracleConnection connection;
            if (IsOtherConn) connection = new OracleConnection(GetJXConnectionString());
            else connection = new OracleConnection(GetConnectionString());
            using (OracleCommand cmd = new OracleCommand())
            {
                try
                {
                    PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                    int rows = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    return rows;
                }
                catch (OracleException E)
                {
                    //Log.WriteLog(E.Message);
                    throw new Exception(E.Message);
                }
                finally
                {
                    cmd.Dispose();
                    connection.Close();
                }

            }
            // }
            return -1;

        }
        #endregion

        #region 连接串
        /// <summary>
        /// 连接串
        /// </summary>
        /// <returns></returns>
        private static string GetConnectionString()
        {

            string strConn = ConfigurationManager.AppSettings["OracleConnString"];
            return strConn;
        }

        /// <summary>
        /// 连接串解密
        /// </summary>
        /// <returns></returns>
        private static string GetJXConnectionString()
        {
            string strConn = ConfigurationManager.AppSettings["JXOracleConnString"];//JXOracleConnString
            return strConn;
        }
        #endregion


    }
}
